/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vnf.bc;

import edu.upn.sigemfi.plt.bc.CuentaFacadeLocal;
import edu.upn.sigemfi.plt.be.Cuenta;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Administrador
 */
public class CuentaDelegate {

    private CuentaFacadeLocal local;

    public CuentaDelegate(){
        local = lookupCuentaFacade();
    }

    public List<Cuenta> listar() throws Exception{
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupCuentaFacade();
            throw new Exception("Listar Cuentas." + e.getMessage(), e);
        }
    }

    private CuentaFacadeLocal lookupCuentaFacade() {
        try {
            Context c = new InitialContext();
            return (CuentaFacadeLocal) c.lookup("java:comp/env/CuentaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public Cuenta buscarPorId(Long id) throws Exception {
        Cuenta cuenta = null;
        try {
            cuenta = local.find(id);
        } catch (Exception ex) {
            local = lookupCuentaFacade();
            throw new Exception("Buscar Cuenta... " + ex.getMessage());
        }
        return cuenta;
    }

 public List<Cuenta> getNrosCuentas(String ruc) throws Exception {
        List<Cuenta> cuentasCliente;
        try {
            cuentasCliente = local.buscarCuentasPorRuc(ruc);
        } catch (Exception ex) {
            local = lookupCuentaFacade();
            throw new Exception("Listar cuentas cliente... " + ex.getMessage());
        }
        return cuentasCliente;
    }

   
}
